package cn.fjsay.java.demo1;

import java.sql.*;

public abstract class BaseDAO {
    protected Connection getConn(){
        try{
            Class.forName(DBInfo.DRIVER);
            return DriverManager.getConnection(DBInfo.URL,DBInfo.USER,DBInfo.PWD);

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void close(ResultSet rs, Statement pstm,Connection conn){
        try {
            if(rs!=null){
                rs.close();
            }
            if(pstm!=null){
                pstm.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    protected boolean executeUpdate(String sql,Object... params){
        Connection conn=null;
        PreparedStatement pstm = null;

        try{
            conn=getConn();
            pstm = conn.prepareStatement(sql);
            for (int i=0;i<params.length;i++){
                pstm.setObject(i+1,params[i]);
            }
            int count=pstm.executeUpdate();
            return count>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(null,pstm,conn);
        }
        return false;

    }

    public abstract boolean addPerson(Person person);

    public abstract boolean updatePerson(Person person);
}



//package cn.fjsay.java.demo1;
//
//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.PreparedStatement;
//import java.sql.SQLException;
//
//public class BaseDAO {
//    // 获取数据库连接
//    public Connection getConn() {
//        Connection conn = null;
//        try {
//            // 加载驱动
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            // 获取连接
//            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_person", "root", "123456");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return conn;
//    }
//
//    // 执行更新操作
//    public boolean executeUpdate(String sql, Object... params) {
//        boolean success = false;
//        Connection conn = getConn();
//        if (conn == null) {
//            return success;
//        }
//        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
//            for (int i = 0; i < params.length; i++) {
//                pstmt.setObject(i + 1, params[i]);
//            }
//            success = pstmt.executeUpdate() > 0;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (conn != null) {
//                    conn.close();
//                }
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//        return success;
//    }
//}